package api

import (
	"net/http"
	"takeout-api/basic/config"
	__ "takeout-api/basic/proto"
	"takeout-api/handler/req"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Code int64       `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

// OrderAdd 订单创建
func OrderAdd(c *gin.Context) {
	var so req.OrderAddReq
	err := c.ShouldBind(&so)
	if err != nil {
		c.JSON(http.StatusBadRequest, &Response{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	add, err := config.TakeoutClient.OrderAdd(c, &__.OrderAddReq{
		MerchantId: uint64(so.MerchantId),
		UserId:     uint64(so.UserId),
		ShopId:     uint64(so.ShopId),
		RiderId:    uint64(so.RiderId),
		Address:    so.Address,
		Stock:      so.Stock,
		Status:     so.Status,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, &Response{
			Code: 500,
			Msg:  "订单生成失败",
			Data: err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, &Response{
		Code: 200,
		Msg:  "创建订单成功",
		Data: add,
	})
}

// MerchantList 商家查看订单列表
func MerchantList(c *gin.Context) {
	var so req.MerchantListReq
	err := c.ShouldBind(&so)
	if err != nil {
		c.JSON(http.StatusBadRequest, &Response{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	add, err := config.TakeoutClient.MerchantList(c, &__.MerchantListReq{
		MerchantId: uint64(so.MerchantId),
		Page:       so.Page,
		Size:       so.Size,
	})

	c.JSON(http.StatusOK, &Response{
		Code: 200,
		Msg:  "商家查看订单列表成功",
		Data: add,
	})
}

// RiderList 骑手查看订单列表
func RiderList(c *gin.Context) {
	var so req.RiderListReq
	err := c.ShouldBind(&so)
	if err != nil {
		c.JSON(http.StatusBadRequest, &Response{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	add, err := config.TakeoutClient.RiderList(c, &__.RiderListReq{
		RiderId: uint64(so.RiderId),
		Page:    so.Page,
		Size:    so.Size,
	})

	c.JSON(http.StatusOK, &Response{
		Code: 200,
		Msg:  "骑手查看订单列表成功",
		Data: add,
	})
}
